package com.kede.api.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.kede.api.auth.domain.LoginUser;
import com.kede.api.auth.domain.User;
import com.kede.api.auth.mapper.MenuMapper;
import com.kede.api.auth.mapper.UserMapper;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/***
 ** @Author:Pr.li
 ** @Description:
 ** @Date: create in 2023/3/31 14:43
 ***/

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private MenuMapper menuMapper;


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        User user = userMapper.selectOne(userLambdaQueryWrapper.eq(User::getUserName, username));

        if (Objects.isNull(user)) {
            throw new RuntimeException("用户或密码错误");
        }
        List<String> list =menuMapper.selectPermsByUserId(user.getId());
        return new LoginUser(user,list);
    }
}
